package ua.kharkov.knure.pereverziev.diploma.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import ua.kharkov.knure.pereverziev.diploma.i18n.RManager;
import ua.kharkov.knure.pereverziev.diploma.web.util.ConstantsUtility;
import ua.kharkov.knure.pereverziev.diploma.web.util.ReportUtil;

/**
 * Context listener
 * 
 * @author A.Pereverziev
 * 
 */
public class ContextListener implements ServletContextListener {

	private static final Logger log = Logger.getLogger(ContextListener.class);

	@Override
	public void contextDestroyed(final ServletContextEvent event) {
		log("Servlet context destruction starts");
		// do nothing
		log("Servlet context destruction finished");
	}

	@Override
	public void contextInitialized(final ServletContextEvent event) {
		log("Servlet context initialization starts");

		final ServletContext servletContext = event.getServletContext();
		initLog4J(servletContext);
		ConstantsUtility.loadAirports(servletContext);
		ConstantsUtility.loadServices(servletContext);
		new ReportUtil().compileReports(servletContext);
		initResourceManager(servletContext);
		setApplicationAttributes(servletContext);

		// InitialDatabaseFilling.insertDataMySQL();
		// FlightsCreator.createFlightForMonth(1);

		log("Servlet context initialization finished");

	}

	/**
	 * Initializes log4j framework.
	 * 
	 * @param servletContext
	 */
	private void initLog4J(final ServletContext servletContext) {
		log("Log4J initialization started");
		try {
			final String log4jConfigFilePath = "WEB-INF/log4j.properties";
			PropertyConfigurator.configure(servletContext
					.getRealPath(log4jConfigFilePath));
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
		log("Log4J initialization finished");
	}

	/**
	 * Sets the global application attributes.
	 * 
	 * @param servletContext
	 */
	private void setApplicationAttributes(final ServletContext servletContext) {

		// servletContext.setAttribute("ticketBean", ticketBean);
		// log.debug("Set the application attribute: ticketBean");

		// set locales map as the application attribute
		servletContext.setAttribute("locales", RManager.getLocales());
		log.debug("Set the application attribute: locales --> "
				+ RManager.getLocales());

		// set bundles
		servletContext.setAttribute("bundles", RManager.getBundles());
		log.debug("Set the application attribute: bundles --> "
				+ RManager.getBundles());

	}

	private void initResourceManager(final ServletContext servletContext) {
		final String localeNames = servletContext
				.getInitParameter("locale-names");
		log.debug("Context parameter: locale-names --> " + localeNames);

		final String bundleName = servletContext
				.getInitParameter("bundle-name");
		log.debug("Context parameter: bundle-name --> " + bundleName);

		RManager.init(bundleName, localeNames);
	}

	private void log(final String msg) {
		System.out.println("[ContextListener] " + msg);
	}
}